package net.algid.purchase.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.algid.purchase.valueObject.Supplier;

public class SupplierDAO extends AbstractDAO implements
		IValueObjectDAO<Supplier> {

	private static int lastCode = 0;

	SupplierDAO(Connection connection) {
		super(connection);
		StringBuilder sql = new StringBuilder().append("SELECT MAX(")
				.append(Supplier.codeField).append(") FROM ")
				.append(Supplier.tableName);
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			statement = connection.prepareStatement(sql.toString());
			resultSet = statement.executeQuery();
			if (resultSet.next()) {
				lastCode = resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeResultSet(resultSet);
			closeStatement(statement);
		}
	}

	@Override
	public Supplier getObject(int code) {
		StringBuilder sql = new StringBuilder().append("SELECT ")
				.append(Supplier.codeField).append(", ")
				.append(Supplier.nameField).append(" FROM ")
				.append(Supplier.tableName).append(" WHERE ")
				.append(Supplier.codeField).append(" = ?");
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			statement = connection.prepareStatement(sql.toString());
			statement.setInt(1, code);
			resultSet = statement.executeQuery();
			if (resultSet.next()) {
				Supplier supplier = new Supplier();
				supplier.code = resultSet.getInt(Supplier.codeField);
				supplier.name = resultSet.getString(Supplier.nameField);
				return supplier;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeStatement(statement);
			closeResultSet(resultSet);
		}

		return null;
	}

	@Override
	public void remove(int code) {
		StringBuilder sql = new StringBuilder().append("DELETE FROM ")
				.append(Supplier.tableName).append(" WHERE ")
				.append(Supplier.codeField).append(" = ?");
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql.toString());
			statement.setInt(1, code);
			statement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeStatement(statement);
		}
	}

	@Override
	public void save(Supplier anObject) {
		if (anObject == null) {
			return;
		}
		StringBuilder sql = new StringBuilder().append("UPDATE ")
				.append(Supplier.tableName).append(" SET ")
				.append(Supplier.nameField).append(" = ? ").append(" WHERE ")
				.append(Supplier.codeField).append(" = ?");
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql.toString());
			statement.setString(1, anObject.name);
			statement.setInt(2, anObject.code);
			statement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeStatement(statement);
		}
	}

	@Override
	public List<Supplier> getList(Supplier anFilter) {
		StringBuilder sql = new StringBuilder().append("SELECT ")
				.append(Supplier.codeField).append(", ")
				.append(Supplier.nameField).append(" FROM ")
				.append(Supplier.tableName);
		StringBuilder condition = new StringBuilder();
		int acceptedFields = 0;
		if (anFilter != null && anFilter.code != Integer.MIN_VALUE) {
			if (acceptedFields > 0)
				condition.append(" AND ");
			condition.append(Supplier.codeField).append(" = ")
					.append(anFilter.code);
			acceptedFields++;
		}
		if (anFilter != null && anFilter.name != null) {
			if (acceptedFields > 0)
				condition.append(" AND ");
			condition.append(Supplier.nameField).append(" = ")
					.append(anFilter.name);
			acceptedFields++;
		}
		if (acceptedFields > 0) {
			sql.append(" WHERE ").append(condition);
		}

		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			statement = connection.prepareStatement(sql.toString());
			resultSet = statement.executeQuery();
			List<Supplier> list = new ArrayList<Supplier>();
			while (resultSet.next()) {
				Supplier supplier = new Supplier();
				supplier.code = resultSet.getInt(Supplier.codeField);
				supplier.name = resultSet.getString(Supplier.nameField);
				list.add(supplier);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeStatement(statement);
			closeResultSet(resultSet);
		}

		return null;
	}

	@Override
	public int add(Supplier anObject) {
		StringBuilder sql = new StringBuilder().append("INSERT INTO ")
				.append(Supplier.tableName).append(" (")
				.append(Supplier.codeField).append(", ")
				.append(Supplier.nameField).append(") VALUES (?, ?)");
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement(sql.toString());
			statement.setInt(1, ++lastCode);
			statement.setString(2, anObject.name);
			statement.executeUpdate();
			return lastCode;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeStatement(statement);
		}

		return Integer.MIN_VALUE;
	}

}
